import numpy as np
import pandas as pd 


def mean_variance_min_max(data_list):
    # 计算均值
    mean_value = np.mean(data_list)
    # 计算方差
    variance = np.var(data_list)

        # 使用 round 函数保留三位小数
    mean_value = round(mean_value, 3)
    variance = round(variance, 3)
    min_value=round(min(data_list),3)
    max_value=round(max(data_list),3)
    return  mean_value,variance,min_value,max_value



print("Mean: ")
print("Variance")   
print("dtw")


def read_txt(filename,data_array):
    # 打开文件
    with open(filename, 'r') as file:
        # 逐行读取
        for i, line in enumerate(file):
            if i >=2000:
                break

            # 去除行尾的换行符
            line = line.strip()
            # 使用空格分隔每一行
            row = line.split(' ')
            for j,value in enumerate(row):
                data_array[i][j]=value

        return data_array
    

# 文件名
filename = '/home/xjz/catkin_ws/src/my_get_model_state_pkg/scripts/isaacgym_obs_action/obs_data.txt'
obs_data_array = np.zeros((2000, 48), dtype=np.float64)
obs_data_array=read_txt(filename,obs_data_array)


mean_value_list,variance_list,min_value_list,max_value_list=[],[],[],[]

for j in  range(obs_data_array.shape[1]):
    data_list=obs_data_array[:,j].tolist()
    mean_value,variance,min_value,max_value=mean_variance_min_max(data_list)
    mean_value_list.append(mean_value)
    variance_list.append(variance)
    min_value_list.append(min_value)
    max_value_list.append(max_value)


# 将列表转换为pandas DataFrame，并指定列名
df = pd.DataFrame({
    "mean": mean_value_list,
    "variance": variance_list,
    "min_value": min_value_list,
    "max_value": max_value_list
})

# 将DataFrame保存为CSV文件
df.to_csv('output.csv', index=False)

from dtw import dtw

manhattan_distance = lambda x, y: np.abs(x - y)
series_1 = np.array([2, 3, 4, 5, 6])
series_2 = np.array([2, 3, 4])

d, cost_matrix, acc_cost_matrix, path = dtw(series_1, series_2, dist=manhattan_distance)


print(d)